'use strict';

const { BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');

/**
 * 窗口控制器
 * @class
 */
class WindowController {

    /**
     * 创建新窗口
     */
    async create(args) {
        const { title = '新窗口', width = 800, height = 600, url: windowUrl, data } = args;
        
        // 创建浏览器窗口
        const win = new BrowserWindow({
            width: width,
            height: height,
            title: title,
            webPreferences: {
                nodeIntegration: true,
                contextIsolation: false,
                enableRemoteModule: true,
                webSecurity: false, // 关闭web安全限制以解决图片显示问题
                allowRunningInsecureContent: false, // 禁止运行不安全的内容
                sandbox: false, // 因为需要nodeIntegration，所以禁用沙箱
                // 添加额外的安全参数
                additionalArguments: [
                    `--disable-features=OutOfBlinkCors`,
                    `--disable-site-isolation-trials`,
                    `--disable-features=BlockInsecurePrivateNetworkRequests`,
                ]
            }
        });
        
        // 加载页面
        win.loadURL(windowUrl);
        
        // 如果有数据，发送给新窗口
        if (data) {
            win.webContents.on('did-finish-load', () => {
                win.webContents.send('window-data', data);
            });
        }
        
        return { success: true, id: win.id };
    }
    
    /**
     * 处理on-open-event事件
     */
    async openEvent(args) {
        // 解构参数
        const { title = '聊天窗口', width = 800, height = 600, url: contentUrl, index } = args;
        
        // 获取应用根目录
        const appPath = process.cwd();
        
        // 创建浏览器窗口
        const win = new BrowserWindow({
            width: width,
            height: height,
            title: title,
            webPreferences: {
                nodeIntegration: true,
                contextIsolation: false,
                enableRemoteModule: true,
                webSecurity: false, // 关闭web安全限制以解决图片显示问题
                allowRunningInsecureContent: false, // 禁止运行不安全的内容
                sandbox: false, // 因为需要nodeIntegration，所以禁用沙箱
                // 添加额外的安全参数
                additionalArguments: [
                    `--disable-features=OutOfBlinkCors`,
                    `--disable-site-isolation-trials`,
                    `--disable-features=BlockInsecurePrivateNetworkRequests`,
                ]
            }
        });
        
        // 判断是否是web url
        const isWebUrl = contentUrl && (contentUrl.startsWith('http:') || contentUrl.startsWith('https:'));
        
        if (isWebUrl) {
            // 加载外部URL
            win.loadURL(contentUrl);
        } else if (contentUrl && contentUrl.startsWith('#/')) {
            // 加载应用内路由页面
            const mainUrl = url.format({
                pathname: path.join(appPath, 'public/dist/index.html'),
                protocol: 'file:',
                slashes: true,
                hash: contentUrl.substring(1) // 移除开头的 '#'
            });
            win.loadURL(mainUrl);
        } else {
            // 加载默认页面
            win.loadURL(url.format({
                pathname: path.join(appPath, 'public/dist/index.html'),
                protocol: 'file:',
                slashes: true
            }));
        }
        
        // 窗口关闭时通知渲染进程
        win.on('closed', () => {
            // 通知主窗口窗口已关闭
            BrowserWindow.getAllWindows().forEach(window => {
                if (window.id !== win.id) {
                    window.webContents.send('window-closed', {
                        index: index
                    });
                }
            });
        });
        
        return { success: true, id: win.id };
    }
}

module.exports = WindowController; 